<script>
import BaseUrl from "@/http/request/BaseUrl";
import TokenAxios from "@/http/request/TokenAxios";

export default {
  data() {
    return {
      showImagedetail: false,
      showVideoDetail:false,
      // 当前每页显示多少条数据
      pageSize: 6,
      //分页相关数据
      currentPage: this.$router.currentRoute.query.page ? parseInt(this.$router.currentRoute.query.page) : 1,
      tableData: [],
      total: 0,
      kw: '',
      url: [],
      content: '',
    }
  },

  methods: {
    selectImagedetail(video) {
      this.showImagedetail = true;
      console.log(video.id);
      this.content = video.content;
      let url = BaseUrl.ADMIN_CONTENT + "/admin/video/" + video.id + "/picture";
      TokenAxios.get(url).then((response) => {
        let JsonResult = response.data;
        if (JsonResult.state == 20000) {
          this.url = JsonResult.data; // 将返回的数据赋值给picUrl数组
          console.log(JsonResult.data)
        } else {
          // 处理状态不为 20000 的情况
        }
      }).catch((error) => {
        // 处理请求错误
        console.error(error);
      });
    },
      selectVideoDetail(video) {
          this.showVideoDetail = true;
          console.log(video.id);
          this.content = video.content;
          let url = BaseUrl.ADMIN_CONTENT + "/admin/video/" + video.id + "/picture";
          TokenAxios.get(url).then((response) => {
              let JsonResult = response.data;
              if (JsonResult.state == 20000) {
                  this.url = JsonResult.data; // 将返回的数据赋值给picUrl数组
                  console.log(JsonResult.data)
              } else {
                  // 处理状态不为 20000 的情况
              }
          }).catch((error) => {
              // 处理请求错误
              console.error(error);
          });
      },
    loadContents() {
      if (location.search.includes('type')) {// /list?type=1/5/8
        let kw = new URLSearchParams(location.search).get('kw');
        let url = BaseUrl.ADMIN_CONTENT + '/admin/video/' + kw + '/search'; // 删除无效的拼接
        TokenAxios.get(url, {params: {kw}}) // 通过 params 选项传递关键字
            .then((response) => {
              let jsonResult = response.data;
              if (jsonResult.state == 20000) {
                this.tableData = jsonResult.data.list;
              } else {
                // 处理状态不为 20000 的情况
              }
            })
      } else {
      }
    },
    //测一下能不能复用
    auditStatus(key, note) {
      console.log(note)
      if (key == 1) {
        let audit = {
          auditStatus: key,
          id: note.id
        }
        let url = BaseUrl.ADMIN_CONTENT + '/admin/video/pass';
        TokenAxios.post(url, audit).then((response) => {
          let jsonResult = response.data;
          if (jsonResult.state == 20000) {
            this.$message({
              message: '审核通过！',
              type: 'success'
            })
            location.reload()
          } else {
            this.$alert(jsonResult.message, '错误', {
              confirmButtonText: '确定',
              callback: action => {
              }
            });
          }
        })
      } else if (key == 2) {
        let auditNoPass = {
          auditStatus: key,
          id: note.id
        }
        let url = BaseUrl.ADMIN_CONTENT + '/admin/video/pass';
        TokenAxios.post(url, auditNoPass).then((response) => {
          let jsonResult = response.data;
          if (jsonResult.state == 20000) {
            this.$message({
              message: '审核不通过！',
              type: 'success'
            })
            location.reload()
          } else {
            this.$alert(jsonResult.message, '错误', {
              confirmButtonText: '确定',
              callback: action => {
              }
            });
          }
        })
      } else {
        this.$alert(jsonResult.message, '查看控制台错误', {
          confirmButtonText: '确定',
          callback: action => {
          }
        });
      }
    },
    //分页
    //切换分页
    handleCurrentChange(page) {
      this.$router.replace('?page=' + page);
      this.loadUserList();
    },
    loadUserList() {
      let page = this.$router.currentRoute.query.page;
      if (!page) {
        page = 1;
      }

      let url = BaseUrl.ADMIN_CONTENT + '/admin/video?page=' + page;
      console.log('url=' + url);

      TokenAxios.get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data.list;
          console.log(this.tableData)
          this.currentPage = jsonResult.data.currentPage;
          this.pageSize = jsonResult.data.pageSize;
          this.total = jsonResult.data.total;
        } else {
          let title = '操作失败';
          this.$alert(jsonResult.message, title, {
            confirmButtonText: '确定',
            callback: action => {
            }
          })
        }
      })
    }
  },
  mounted() {
    this.loadUserList();
    var data = localStorage.getItem("loginInfo")
    var parse = JSON.parse(data);
    this.headers.Authorization = parse.token
    this.loadContents();
  },
  beforeRouteUpdate(to, from, next) {
    //此方法在路由跳转之前执行,如果不执行里面的next则拦截此次跳转
    next();//放行
    this.loadContents();
  }
}
</script>

<template>
  <div slot="header" class="clearfix">
    <!--面包屑导航区域-->
    <el-breadcrumb separator-class="el-icon-arrow-right" style="margin-bottom: 20px;overflow: hidden">
      <el-breadcrumb-item :to="{ path: '/admin/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>視頻管理</el-breadcrumb-item>
      <el-breadcrumb-item>視頻审核</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="main-card">
      <!-- 文章分类 -->
      <el-select
          clearable
          size="small"
          filterable
          placeholder="请选择分类"
          style="margin-right:1rem"
      >
        <el-option
            v-for="item in categoryList"
            :key="item.id"
            :label="item.categoryName"
            :value="item.categoryId"
        />
      </el-select>
      <!-- 文章名 -->
      <el-input
          clearable
          prefix-icon="el-icon-search"
          size="small"
          placeholder="请输入文章名"
          style="width:200px"
          v-model="kw"
          @keyup.enter.native="loadContents"
      />

      <el-button
          type="primary"
          size="small"
          icon="el-icon-search"
          style="margin-left:1rem"
          @click="searchArticles"
      >
        搜索
      </el-button>

      <el-table :data="tableData"
                style="width: 98%; margin-top:20px;">
        <el-table-column align="center" label="ID" prop="id">
        </el-table-column>
        <el-table-column align="center" label="用戶名" prop="authorName"
                         :show-overflow-tooltip="true">
        </el-table-column>
        <el-table-column label="視頻封面" width="180" align="center" prop="coverUrl">
          <template slot-scope="scope">
            <div style="width: 115px;height: 115px;overflow: hidden;position: relative;top: 18px">
              <img :src="scope.row.coverUrl" v-if="scope.row.resourcesType !== 1" width="100%" height="100%"
                   style="object-fit: cover;">
              <video :src="scope.row.url" v-else style="object-fit: cover;width: 100%; height: 100%;"/>
            </div>
            <br>
            <el-button type="text" @click="selectImagedetail(scope.row)" style="margin-right: 55px">查看详细</el-button>
            <el-dialog
                title="封面图片"
                :visible.sync="showImagedetail"
                width="50%"
                center
                @close="handleDialogClose"
            >
              <el-row>
                <el-col span:12>
                  <el-carousel trigger="click" height="600px">
                      <el-image fit="scale-down" :src="url.coverUrl" style="width: 100%; height: 100%"/>
                  </el-carousel>
                </el-col>
                <el-col span:12>
                  <p>{{ content }}</p>
                </el-col>
              </el-row>
            </el-dialog>
          </template>
        </el-table-column>


        <!-- 文章标题 -->
        <el-table-column prop="title" label="标题" align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <!-- 文章分类 -->
        <el-table-column prop="categoryName" label="分类" width="110px" align="center"
                         :show-overflow-tooltip="true"></el-table-column>


<!--        &lt;!&ndash; 文章ip &ndash;&gt;-->
<!--        <el-table-column-->
<!--            prop="ip"-->
<!--            label="ip"-->
<!--            align="center"-->
<!--        ></el-table-column>-->

          <el-table-column label="視頻" width="180" align="center" prop="videoUrl">
              <template slot-scope="scope">
                  <div style="width: 115px;height: 115px;overflow: hidden;position: relative;top: 18px">
                      <video :src="scope.row.videoUrl" style="object-fit: cover;width: 100%; height: 100%;"/>
                  </div>
                  <br>
                  <el-button type="text" @click="selectVideoDetail(scope.row)" style="margin-right: 55px">查看详细</el-button>
                  <el-dialog
                          title="視頻"
                          :visible.sync="showVideoDetail"
                          width="50%"
                          center
                          @close="handleDialogClose"
                  >
                      <el-row>
                          <el-col span:12>
                              <el-carousel trigger="click" height="600px">
                                  <el-carousel-item>
                                      <div style="width: 885px;height: 600px;background-color: black;">
                                          <video :src="url.videoUrl"
                                                 style="width: 100%;height: 100%;"
                                                 controls
                                                 autoplay/>
                                      </div>
                                  </el-carousel-item>
                              </el-carousel>
                          </el-col>
                          <el-col span:12>
                              <p>{{ content }}</p>
                          </el-col>
                      </el-row>
                  </el-dialog>
              </template>
          </el-table-column>


        <el-table-column prop="gmtCreate" label="创建时间" align="center" :show-overflow-tooltip="true"></el-table-column>

        <el-table-column align="center" label="操作" width="180">
          <template slot-scope="scope">
           <span>
            <el-button style="width: 100px;" type="primary" @click="auditStatus(1,scope.row)">审核通过</el-button>
            <el-button style="width: 100px;margin-right: 10px" type="danger"
                       @click="auditStatus(2,scope.row)">审核不通过</el-button>
           </span>
          </template>
        </el-table-column>

      </el-table>

      <!-- 分页 -->
      <el-pagination
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :hide-on-single-page="true"
          :page-size="pageSize"
          layout="total, prev, pager, next,jumper"
          :total="total"
          background
      />
    </el-card>
  </div>
</template>

<style scoped>
.pagination-container {
  float: right;
  margin-top: 1.25rem;
  margin-bottom: 1.25rem;
}

.box-card {
  width: 98%;
  margin: 1%;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}

.clearfix:after {
  clear: both
}

.clearfix span {
  font-weight: 600;
}

.el-carousel__item h3 {
  color: #475669;
  font-size: 14px;
  opacity: 0.75;
  line-height: 150px;
  margin: 0;
}

.el-carousel__item:nth-child(2n) {
  background-color: #99a9bf;
}

.el-carousel__item:nth-child(2n+1) {
  background-color: #d3dce6;
}
</style>